<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('商品列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>商品名称：</label>
                                <input type="text" name="name"/>
                            </li>
                            <li>
                                <label>价格区间：</label>
                                <select name="params[priceFlag]">
                                    <option value="">所有</option>
                                    <option value="1">100以下</option>
                                    <option value="2">100~500</option>
                                    <option value="3">500~1000</option>
                                    <option value="4">1000以上</option>
                                </select>
                            </li>
                            <li>
                                商品类型：<select name="type" id="selectType">
                                <option value="">所有</option>
                            </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="admin:cosmetics:add">
                    <i class="fa fa-plus"></i> 添加
                </a>
                <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="admin:cosmetics:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="admin:cosmetics:remove">
                    <i class="fa fa-remove"></i> 删除
                </a>
                <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="admin:cosmetics:export">
                    <i class="fa fa-download"></i> 导出
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('admin:cosmetics:edit')}]];
        var removeFlag = [[${@permission.hasPermi('admin:cosmetics:remove')}]];
        var prefix = ctx + "admin/cosmetics";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                detailView: true,
                detailFormatter: detailFormatter,
                modalName: "商品",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'cosId',
                    title: '化妆品商品唯一标识',
                    visible: false
                },
                {
                    field: 'name',
                    title: '商品名称',
                    cellStyle: function (value, row, index) {
                        return {
                            css: {
                                'font-weight': 'bold'
                            }
                        };
                    }
                },
                {
                    field: 'cosType.name',
                    title: '商品种类'
                },
                {
                    field: 'image',
                    title: '图片',
                    formatter: function(value, row, index) {
                        return $.table.imageView(row.image,300,400);
                    }
                },
                {
                    field: 'price',
                    title: '商品价格',
                    formatter: function (value, row, index) {
                        return '￥'+row.price;
                    }
                },
                {
                    field: '',
                    title: '剩余数量',
                    formatter: function (value, row, index) {
                        return row.number+' ('+ row.unit +')';
                    }
                },
                {
                    field: 'status',
                    title: '商品状态',
                    formatter: function (value, row, index) {
                        return statusTools(row);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.cosId + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.cosId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.table.init(options);

            //类型下拉框初始化
            let view = $('#selectType');
            $.ajax({
                url:ctx + 'admin/type/listNotPage',
                type:'post',
                dataType:'json',
                success: function(result) {
                    result.rows.map( item => {
                        view.append('<option value="'+ item.typeId +'">'+ item.name +'</option>');
                    });
                }
            });
        });

        /* 商品状态显示 */
        function statusTools(row) {
            if (row.status == 2) {
                return '<i class=\"fa fa-toggle-off text-info fa-2x\" onclick="enable(\'' + row.cosId + '\')"></i> ';
            } else {
                return '<i class=\"fa fa-toggle-on text-info fa-2x\" onclick="disable(\'' + row.cosId + '\')"></i> ';
            }
        }

        /* 商品管理-停用 */
        function disable(cosId) {
            $.modal.confirm("确认要停用商品吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "cosId": cosId, "status": 2 });
            })
        }

        /* 商品管理-启用 */
        function enable(cosId) {
            $.modal.confirm("确认要启用商品吗？", function() {
                $.operate.post(prefix + "/changeStatus", { "cosId": cosId, "status": 1 });
            })
        }

        //商品信息详情
        function detailFormatter(index, row) {
            let html = [];
            html.push('<p><b>商品简介: </b> '+ row.introduce);
            html.push('<p><b>更新时间: </b> '+ row.updateDate);
            return html.join('');
        }
    </script>
</body>
</html>